/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Windows7
 */
public class ProjectSettings {

    private static ProjectSettings instance;
    private String configsFilePath;
    private List<SecurityUrl> sercurityUrls;
    private String persistenceUnitName;
    private boolean useProxy;
    private String serverProxyAddress;
    private int serverProxyPort;
    private int maxSizeImage;
    private int maxPhotoInAlbum;

    /**
     * @return the instance
     */
    public static ProjectSettings getInstance() {
        if (instance == null) {
            instance = new ProjectSettings();
        }
        return instance;
    }

    private ProjectSettings() {
    }

    public int getMaxWidthDetailsPhoto() {
        return 750;
    }

    public void loadConfigs() {
        loadConfigs(configsFilePath);
    }

    public void loadConfigs(String configsFilePath) {
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);

            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(configsFilePath);

            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath.compile("//configs/jpa-settings/persistence-unit-name/text()");
            persistenceUnitName = expr.evaluate(doc);
            expr = xpath.compile("//configs/proxy-settings/enable/text()");
            useProxy = Boolean.parseBoolean(expr.evaluate(doc));
            expr = xpath.compile("//configs/proxy-settings/server-address/text()");
            serverProxyAddress = expr.evaluate(doc);
            expr = xpath.compile("//configs/proxy-settings/port/text()");
            serverProxyPort = Integer.parseInt(expr.evaluate(doc));
            expr = xpath.compile("//configs/album-settings/max-size-of-photo/text()");
            maxSizeImage = Integer.parseInt(expr.evaluate(doc));
            expr = xpath.compile("//configs/album-settings/max-number-photo/text()");
            maxPhotoInAlbum = Integer.parseInt(expr.evaluate(doc));
            expr = xpath.compile("//configs/security-settings/url");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            sercurityUrls = new ArrayList<SecurityUrl>();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                NodeList childNodes = node.getChildNodes();
                SecurityUrl url = new SecurityUrl();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node childNode = childNodes.item(j);
                    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                        if (childNode.getNodeName().equals("pattern")) {
                            url.setPattern(childNode.getTextContent());
                        }
                        if (childNode.getNodeName().equals("authorized")) {
                            url.setAuthorized(Boolean.parseBoolean(childNode.getTextContent()));
                        }
                        if (childNode.getNodeName().equals("roles")) {
                            url.setRoles(Arrays.asList(childNode.getTextContent().toLowerCase().replaceAll(" ", "").split(",")));
                        }
                        if (childNode.getNodeName().equals("login-url")) {
                            url.setLoginUrl(childNode.getTextContent());
                        }
                    }
                }
                sercurityUrls.add(url);
            }
        } catch (XPathExpressionException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }
    }

    public List<SecurityUrl> getSecurityUrls() {
        return sercurityUrls;
    }

    public String getPersistenceUnitName() {
        return persistenceUnitName;
    }

    public boolean isUseProxy() {
        return useProxy;
    }

    public String getServerProxyAddress() {
        return serverProxyAddress;
    }

    public int getServerProxyPort() {
        return serverProxyPort;
    }
    
    public String getUsername(){
        return "webphotosharingteam@gmail.com";
    }
    
    public String getPassword(){
        return "fat123456";
    }
    
    public int getMaxSizeImage(){
        //return 2000000;
        return maxSizeImage;
    }
    
    public int getMaxPhotoInAlbum(){
        //return 2;
        return maxPhotoInAlbum;
    }

}
